package com.shenbianyun.cloudcaring.payment;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.util.*;
import java.util.Map.Entry;

public class MD5SignUtils {
    private static final Logger LOG = LoggerFactory.getLogger(MD5SignUtils.class);

    public MD5SignUtils() {
    }

    public static String getSign(String md5Key, Map param) {
        try {
            return _md5Encode(getSignPlainText(md5Key, param));
        } catch (Exception var3) {
            LOG.error("签名错误", var3);
            return "ERROR";
        }
    }

    public static boolean verifySign(String signKey, Map<String, String> param) {
        String sign = (String)param.get("sign");
        param.remove("sign");
        String verifySign = getSign(signKey, param);
        if (verifySign.equals(sign)) {
            LOG.info("验签通过");
            return true;
        } else {
            LOG.info("验签失败");
            return false;
        }
    }

    public static String getSignPlainText(String md5Key, Map<String, String> param) {
        try {
            if (param.get("sign") != null) {
                param.remove("sign");
            }

            List arrayList = new ArrayList(param.entrySet());
            Collections.sort(arrayList, new Comparator() {
                public int compare(Object o1, Object o2) {
                    Entry obj1 = (Entry)o1;
                    Entry obj2 = (Entry)o2;
                    return obj1.getKey().toString().compareTo((String)obj2.getKey());
                }
            });
            StringBuilder md5keyBuilder = new StringBuilder("");
            Iterator iter = arrayList.iterator();

            while(iter.hasNext()) {
                Entry entry = (Entry)iter.next();
                String value = String.valueOf(entry.getValue());
                if (null!=value && !value.equals("null")) {
                    md5keyBuilder.append(value);
                }
            }

            md5keyBuilder.append(md5Key);
            return md5keyBuilder.toString();
        } catch (Exception var7) {
            LOG.error("组装签名参数错误", var7);
            return "ERROR";
        }
    }

    public static String _md5Encode(String inStr) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();

        for(int i = 0; i < md5Bytes.length; ++i) {
            int val = md5Bytes[i] & 255;
            if (val < 16) {
                hexValue.append("0");
            }

            hexValue.append(Integer.toHexString(val));
        }

        return hexValue.toString();
    }

    public static void main(String[] args) throws Exception {
        Map map = new HashMap();
        map.put("cardNo", "6224210150002400140");
        map.put("idCard", "410203197408261561");
        map.put("userName", "常颖666");
        map.put("bankId", "2");
        map.put("authType", "2");
        map.put("business", "2");
        String sign = getSign("7854987541562314785479658741548", map);
        System.out.println(sign);
    }
}
